package com.example.my_bookkeeping;


import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.is;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import androidx.test.espresso.DataInteraction;
import androidx.test.espresso.ViewInteraction;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;

import com.example.my_bookkeeping.data.manager.BookKeepManager;
import com.example.my_bookkeeping.data.manager.UserManager;
import com.example.my_bookkeeping.data.user.BookKeep;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class AddBookKeepTest {

    @Rule
    public ActivityScenarioRule<MainActivity> mActivityScenarioRule =
            new ActivityScenarioRule<>(MainActivity.class);

    private BookKeepManager bookKeepManager;
    private UserManager userManager;

    @Before
    public void setUp() {
        bookKeepManager = BookKeepManager.getInstance();
        userManager = UserManager.getInstance();
        userManager.reset();
    }

    @After
    public void tearDown() {
        userManager.reset();
    }

    @Test
    public void mainActivityTest2() throws InterruptedException {
        ViewInteraction bottomNavigationItemView = onView(
                allOf(withId(R.id.item_record), withContentDescription("添加账目"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.bottom_navigation_view),
                                        0),
                                2),
                        isDisplayed()));
        bottomNavigationItemView.perform(click());

        Thread.sleep(1000);

        ViewInteraction tabView = onView(
                allOf(withContentDescription("支出"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.tabLayout_type),
                                        0),
                                0),
                        isDisplayed()));
        tabView.perform(click());

        ViewInteraction appCompatEditText = onView(
                allOf(withId(R.id.editText_amount),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.main),
                                        1),
                                0),
                        isDisplayed()));
        appCompatEditText.perform(replaceText("128.50"), closeSoftKeyboard());

        ViewInteraction appCompatSpinner = onView(
                allOf(withId(R.id.spinner_category),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.main),
                                        1),
                                1),
                        isDisplayed()));
        appCompatSpinner.perform(click());

        DataInteraction appCompatTextView = onData(anything())
                .inAdapterView(childAtPosition(
                        withClassName(is("android.widget.PopupWindow$PopupBackgroundView")),
                        0))
                .atPosition(1);
        appCompatTextView.perform(click());

        ViewInteraction appCompatEditText2 = onView(
                allOf(withId(R.id.editText_note),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.main),
                                        1),
                                2),
                        isDisplayed()));
        appCompatEditText2.perform(replaceText("星巴克咖啡"), closeSoftKeyboard());

        ViewInteraction appCompatEditText3 = onView(
                allOf(withId(R.id.editTextDate_datetime),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.main),
                                        1),
                                3),
                        isDisplayed()));
        appCompatEditText3.perform(replaceText("2025.05.01"), closeSoftKeyboard());

        ViewInteraction appCompatButton = onView(
                allOf(withId(R.id.button_save), withText("添加"),
                        childAtPosition(
                                allOf(withId(R.id.main),
                                        childAtPosition(
                                                withId(android.R.id.content),
                                                0)),
                                3),
                        isDisplayed()));
        appCompatButton.perform(click());

        ArrayList<BookKeep> bookKeeps = bookKeepManager.getAll();

        assert bookKeeps.size() == 1;
        BookKeep bookKeep = bookKeeps.get(0);

        assert bookKeep != null;
        assert bookKeep.getAmount() == 128.50;
        assert !bookKeep.getIsIncome();
        assert bookKeep.getCategory().equals("餐饮");
        assert bookKeep.getRemark().equals("星巴克咖啡");
        assert bookKeep.getTime().equals("2025.05.01");
    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}
